package com.lanou.musicbox.fragment.seekmusic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.seekmusic.EnterThe_Singer_Adapter;
import com.lanou.musicbox.bean.SingerContentEntity;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.entity.Entity;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;


import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 赵宝龙 on 15/10/23.
 * 进入搜索_进入歌手页面
 */
public class EnterThe_Singer_Fragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageView singerImg;
    private ListView songerList;
    private String url;
    private RequestQueue requestQueue;
    private List<SingerContentEntity.DataEntity> data;
    private EnterThe_Singer_Adapter adapter;
    private String song_link;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.enterthe_singer_list, null);
        singerImg = (ImageView) view.findViewById(R.id.singer_img);
        songerList = (ListView) view.findViewById(R.id.songer_list);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        singerImg.setOnClickListener(this);
        songerList.setOnItemClickListener(this);
        adapter = new EnterThe_Singer_Adapter(getActivity());
        Bundle bundle = getArguments();
        int id = bundle.getInt("id");
        url = Contant.Url.URL_Seek_Singger_first + id +Contant.Url.URL_Seek_Singger_end;

        getInternetData();
    }

    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SingerContentEntity entity = gson.fromJson(s, SingerContentEntity.class);
                if (entity != null && entity.getData() != null) {
                    data = entity.getData();
                    songerList.setAdapter(adapter);
                    adapter.setDatas(data);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<Entity> songsInfos = new ArrayList<>();
        if (data.get(position).getAuditionList()!=null
                &&data.get(position).getAuditionList().size()>0)
        {
            for (int i = 0; i < data.size(); i++) {
                Entity netSongs = new Entity();

                if (data.get(i).getAuditionList()!=null&&data.get(i).getAuditionList().size()>0){
                    song_link = data.get(i).getAuditionList().get(1).getUrl();
                }
                else {
                    song_link = "";
                }

                String title = data.get(i).getName();
                String author = data.get(i).getSingerName();
                netSongs.setUrl(song_link);
//                netSongs.setSmall_pic(bg_img);
//                netSongs.setBig_pic(bg_img);
                netSongs.setTitle(title);

                netSongs.setArtist(author);
                netSongs.setItem_id(position);
                songsInfos.add(i, netSongs);
            }
            String whatMusic = "天天动听";
            Events.MusicInfoEvent musicInfoEvent= new Events.MusicInfoEvent();
            musicInfoEvent.setMp3Infos(songsInfos,whatMusic);
            EventBus.getDefault().post(musicInfoEvent);
        }
        else {
            Toast.makeText(getActivity(), "这首歌没有授权,我也无能为力哦!", Toast.LENGTH_SHORT).show();
        }

    }
}
